﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Manager
{
    public partial class frmNV : Form
    {
        #region properties
        SQL sql = new SQL();
        bool checkAddNV = false;
        string strQuery;
        string strMNV;
        string strNNV;
        string strMPB;
        string strNPB;
        string strMCT;
        string strNCT;

        string sMCV;
        int index;

        #endregion

        public frmNV()
        {
            InitializeComponent();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                index = e.RowIndex;
                strMNV = dataGridView1.Rows[index].Cells[0].Value.ToString().Trim();
                get_Value(index);
                set_read_Only(true);
                btnUpdate.Enabled = false;
                btnEdit.Enabled = true;
                checkAddNV = false;
                btnAddNV.Text = "Thêm nhân viên";
            }
            catch { }
        }

        private void ffrmNV_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            btnEnable(false);
            set_read_Only(true);
            getValueComboboxMPB(cbSearchMPB);
            getValueComboboxMNV(cbSearchMNV);
            getValueComboboxMCT(cbSearchMCT);
        }     
       
        private void button1_Click(object sender, EventArgs e)
        {
            btnEnable(true);
            sql_search_NhanVien();
            btnUpdate.Enabled = false;
        }       

        private void button2_Click(object sender, EventArgs e)
        {
           
            if (checkAddNV == true)
            {
                if (MessageBox.Show("Bạn thực sự muốn insert nhân viên ?", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
                    {
                        SqlParameter[] sqlPara = new SqlParameter[10];
                        sqlPara[0] = new SqlParameter("@MNV", tbMNV.Text);
                        sqlPara[1] = new SqlParameter("@MPB", cbMPB.Text.Trim());
                        sqlPara[2] = new SqlParameter("@ADDRESS", tbAddress.Text);
                        sqlPara[3] = new SqlParameter("@DATEVCT", dateIntoCompany.Value.Date.ToString("MM/dd/yyyy"));
                        sqlPara[4] = new SqlParameter("@BIRTHDAY", dateBirtday.Value.Date.ToString("MM/dd/yyyy"));
                        sqlPara[5] = new SqlParameter("@MCV", cbMCV.Text.Trim());
                        sqlPara[6] = new SqlParameter("@MCT", cbMCT.Text.Trim());
                        sqlPara[7] = new SqlParameter("@DATETG", dateTGCV.Value.Date.ToString("MM/dd/yyyy"));
                        sqlPara[8] = new SqlParameter("@DATEKT", dateKTCV.Value.Date.ToString("MM/dd/yyyy"));
                        sqlPara[9] = new SqlParameter("@NAMENV", tbnameNV.Text);
                        sql.execute_Query("sql_insert_NV", sqlPara);
                        btnAddNV.Text = "Thêm nhân viên";
                        btnUpdate.Enabled = false;
                        btnEdit.Enabled = true;
                        checkAddNV = false;
                        checkAddNV = false;
                        set_read_Only(true);
                        sql_search_NhanVien();
                    }
                
            }
            else
            {
                tbNameCT.Text = "";
                tbnameCV.Text = "";
                cbMCT.Text = "";
                cbMCV.Text = "";
                btnAddNV.Text = "Insert";
                checkAddNV = true;
                getMaNhanVien();                
            }
        }
       
        private void button4_Click(object sender, EventArgs e)
        {
            if (tbMNV.Text != "")
            {
                string strMPB = cbMPB.Text;
                string strNAMECT = tbNameCT.Text;
                string strNAMECV = tbnameCV.Text;
                string strMCT = cbMCT.Text;
                string strMCV = cbMCV.Text;
                string strDKHT = tbDKHT.Text;
                sMCV = strMCV;

                getValueComboboxMPB(cbMPB);
                cbMPB.Text = strMPB;
                getValueComboboxMCT_where(cbMCT);

                cbMCV.DataSource = sql.getDataTable("sql_select_MCV_where_MCT", new SqlParameter("@MCT", strMCT));
                cbMCV.ValueMember = "MCV";
                cbMCT.Text = strMCT;
                cbMCV.Text = strMCV;
                tbnameCV.Text = strNAMECV;
                tbNameCT.Text = strNAMECT;
                tbDKHT.Text = strDKHT;


                set_read_Only(false);
                tbMNV.ReadOnly = true;
                tbnameNV.Focus();
                btnUpdate.Enabled = true;
                btnEdit.Enabled = false;
            }
            else
                MessageBox.Show("Please choose Nhân viên ?", "Notify", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn thực sự muốn update ?", "Question", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                SqlParameter[] sqlPara = new SqlParameter[11];
                sqlPara[0] = new SqlParameter("@MNV", tbMNV.Text);
                sqlPara[1] = new SqlParameter("@MPB", cbMPB.Text.Trim());
                sqlPara[2] = new SqlParameter("@ADDRESS", tbAddress.Text);
                sqlPara[3] = new SqlParameter("@DATEVCT", dateIntoCompany.Value.Date.ToString("MM/dd/yyyy"));
                sqlPara[4] = new SqlParameter("@BIRTHDAY", dateBirtday.Value.Date.ToString("MM/dd/yyyy"));
                sqlPara[5] = new SqlParameter("@MCVN", cbMCV.Text.Trim());
                sqlPara[6] = new SqlParameter("@MCT", cbMCT.Text.Trim());
                sqlPara[7] = new SqlParameter("@DATETG", dateTGCV.Value.Date.ToString("MM/dd/yyyy"));
                sqlPara[8] = new SqlParameter("@DATEKT", dateKTCV.Value.Date.ToString("MM/dd/yyyy"));
                sqlPara[9] = new SqlParameter("@NAMENV", tbnameNV.Text);
                sqlPara[10] = new SqlParameter("@MCVO", sMCV);
                sql.execute_Query("sql_update_NhanVien", sqlPara);
                sql_search_NhanVien();
                set_read_Only(true);
                btnEdit.Enabled = true;
                btnUpdate.Enabled = false;
                tbnameNV.Focus();
            }            
        }

        private void btnDeleteNV_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("Ban chac chan muon xoa ??", "Information", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
                {
                    string mNV = dataGridView1.Rows[index].Cells[0].Value.ToString().Trim();
                    sql.execute_Query("sql_delete_NhanVien", new SqlParameter("@MNV", mNV));
                    sql_search_NhanVien();
                }
            }
            catch 
            {
                
            }
        }      

        private void rbNV_CheckedChanged(object sender, EventArgs e)
        {
            //search NV
            sql_search_NhanVien();
            btnEnable(true);
            btnUpdate.Enabled = false;
            //get value MNV into cbMNV
            cbSearchMNV.DataSource = sql.getDataTable("sql_MNV_cb", new SqlParameter("@checkNV", 1));
            cbSearchMNV.ValueMember = "MNV";
            cbSearchMNV.Text = "";
        }

        private void rbTP_CheckedChanged(object sender, EventArgs e)
        {
            //search TP
            sql_search_NhanVien();
            cbSearchMNV.DataSource = sql.getDataTable("sql_MNV_cb");
            cbSearchMNV.ValueMember = "MNV";
            cbSearchMNV.Text = "";
        }      

       
        public void btnEnable(bool check) 
        {
            btnAddNV.Enabled = check;
            btnDeleteNV.Enabled = check;
            btnEdit.Enabled = check;
            btnUpdate.Enabled = check;
        }
       
        private void cbMCT_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt = sql.getDataTable("sql_select_MCV_where_MCT", new SqlParameter("@MCT", cbMCT.Text));
            if (dt.Rows.Count > 0)
            {
                cbMCV.DataSource = dt;
                cbMCV.ValueMember = "MCV";
                cbMCV.Enabled = true;
                cbMCV.Text = "";
                tbnameCV.Clear();
            }
            
            dt = sql.getDataTable("sql_select_NAMECT_where_MCT", new SqlParameter("@MCT", cbMCT.Text));
            if (dt.Rows.Count > 0)
            {
                tbNameCT.Text = dt.Rows[0][0].ToString();
            }            
        }

        private void cbMCV_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = sql.getDataTable("sql_select_CongViec", new SqlParameter("@MCV", cbMCV.Text));
                tbnameCV.Text = dt.Rows[0][0].ToString();
                tbDKHT.Text = dt.Rows[0][1].ToString();
            }
            catch { }
        }

        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                contextMenu.Show(new Point(Cursor.Position.X, Cursor.Position.Y));
            }
        }

        private void themCongViecToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmAddCV frm = new frmAddCV();
            frm.getMNV(strMNV);            
            if (frm.ShowDialog() == DialogResult.OK) 
            {
                sql_search_NhanVien();
            }
            
        }

        private void cbMPB_SelectedIndexChanged(object sender, EventArgs e)
        {            
            getValueComboboxMCT_where(cbMCT);
            tbNameCT.Text = "";           
        }
        public void get_Value(int index)
        {
            if (check_AllNull_GroupSearch() == false)
            {
                tbMNV.Text = dataGridView1.Rows[index].Cells[0].Value.ToString().Trim();
                cbMPB.Text = dataGridView1.Rows[index].Cells[1].Value.ToString().Trim();
                tbnameNV.Text = dataGridView1.Rows[index].Cells[3].Value.ToString().Trim();
                tbAddress.Text = dataGridView1.Rows[index].Cells[4].Value.ToString().Trim();
                tbNameCT.Text = dataGridView1.Rows[index].Cells[8].Value.ToString().Trim();
                tbnameCV.Text = dataGridView1.Rows[index].Cells[10].Value.ToString().Trim();
                tbDKHT.Text = dataGridView1.Rows[index].Cells[13].Value.ToString().Trim();
                cbMCT.Text = dataGridView1.Rows[index].Cells[7].Value.ToString().Trim();
                cbMCV.Text = dataGridView1.Rows[index].Cells[9].Value.ToString().Trim();
                dateIntoCompany.Value = Convert.ToDateTime(dataGridView1.Rows[index].Cells[5].Value.ToString().Trim());
                dateBirtday.Value = Convert.ToDateTime(dataGridView1.Rows[index].Cells[6].Value.ToString().Trim());
                try
                {
                    dateTGCV.CustomFormat = "";
                    dateTGCV.Value = Convert.ToDateTime(dataGridView1.Rows[index].Cells[11].Value.ToString().Trim());
                }
                catch
                {
                    dateTGCV.Format = DateTimePickerFormat.Custom;
                    dateTGCV.CustomFormat = "MM/dd/yyyyy";
                }
                try
                {
                    dateKTCV.CustomFormat = "";
                    dateKTCV.Value = Convert.ToDateTime(dataGridView1.Rows[index].Cells[12].Value.ToString().Trim());
                }
                catch
                {
                    dateKTCV.Format = DateTimePickerFormat.Custom;
                    dateKTCV.CustomFormat = "MM/dd/yyyyy";
                }
            }
            else 
            {                
                tbMNV.Text = dataGridView1.Rows[index].Cells[0].Value.ToString().Trim();
                cbMPB.Text = dataGridView1.Rows[index].Cells[1].Value.ToString().Trim();
                tbnameNV.Text = dataGridView1.Rows[index].Cells[2].Value.ToString().Trim();
                tbAddress.Text = dataGridView1.Rows[index].Cells[3].Value.ToString().Trim();
                dateIntoCompany.Value = Convert.ToDateTime(dataGridView1.Rows[index].Cells[4].Value.ToString().Trim());
                dateBirtday.Value = Convert.ToDateTime(dataGridView1.Rows[index].Cells[5].Value.ToString().Trim());

            }
        }

        public void set_read_Only(bool check)
        {
            cbMPB.Enabled = !check;
            tbnameNV.ReadOnly = check;
            tbAddress.ReadOnly = check;            
            dateIntoCompany.Enabled = !check;
            dateBirtday.Enabled = !check;
            cbMCT.Enabled = !check;
            cbMCV.Enabled = !check;
            dateKTCV.Enabled = !check;
            dateTGCV.Enabled = !check;
        }

        public bool check_null_GroupInformation() 
        {
            foreach (Control c in groupBox2.Controls)
            {
                if (c.Text == "") 
                {
                    MessageBox.Show("Vui Lòng nhập đầy đủ thông tin ?", "Notify", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return true;
                }
            }
            return false;
        }

        public bool check_AllNull_GroupSearch() 
        {
            int intCh = 0;
            foreach (Control c in groupBox1.Controls)
                if (c.Text == "")
                    intCh++;
            if (intCh == 6)
                return true;
            else
                return false;
        }

        public void clearTextBox()
        {
            tbAddress.Clear();
            tbDKHT.Clear();
            tbMNV.Clear();
            tbNameCT.Clear();
            tbnameCV.Clear();
            tbnameNV.Clear();
            tbMNV.Focus();
            tbNameCT.Clear();
            tbnameCV.Clear();
            cbMPB.Text = "";
            tbnameNV.Focus();
        }

        public void sql_search_NhanVien()
        {
            strMNV =  cbSearchMNV.Text;
            strNNV = tbSearchNameNV.Text;
            strMPB = cbSearchMPB.Text;
            strNPB = tbSearchNamePB.Text;
            strMCT = cbSearchMCT.Text;
            strNCT = tbSearchNameCT.Text;

            DataTable dt = new DataTable();
            dataGridView1.Columns.Clear();   
         
            if (check_AllNull_GroupSearch()==true)
            {
                dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                SqlParameter sqlPara = new SqlParameter();
                if (rbNV.Checked == true)
                    sqlPara = new SqlParameter("@checkNV", 1);
                else
                    sqlPara = new SqlParameter("@checkNV", 0);
                dt = sql.getDataTable("sql_search_NV", sqlPara);
                dt.Columns[0].ColumnName = "Mã nhân viên";
                dt.Columns[1].ColumnName = "Mã phòng ban";
                dt.Columns[2].ColumnName = "Tên nhân viên";
                dt.Columns[3].ColumnName = "Địa chỉ";
                dt.Columns[4].ColumnName = "Ngày vào công ty";
                dt.Columns[5].ColumnName = "Ngày sinh";
                dataGridView1.DataSource = dt;
                cbMCT.Text = "";
                cbMCV.Text = "";
                tbnameCV.Clear();
                tbNameCT.Clear();
                tbDKHT.Clear();
            }
            else
            {
                dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
                SqlParameter[] sqlPara = new SqlParameter[7];
                if (rbNV.Checked == true)
                    sqlPara[0] = new SqlParameter("@checkSearchNV", 1);
                else
                    sqlPara[0] = new SqlParameter("@checkSearchNV", 0);
                sqlPara[1] = new SqlParameter("@maNV", cbSearchMNV.Text.Trim());
                sqlPara[2] = new SqlParameter("@nameNV", tbSearchNameNV.Text);
                sqlPara[3] = new SqlParameter("@maPB", cbSearchMPB.Text.Trim());
                sqlPara[4] = new SqlParameter("@namePB", tbSearchNamePB.Text);
                sqlPara[5] = new SqlParameter("@maCT", cbSearchMCT.Text.Trim());
                sqlPara[6] = new SqlParameter("@nameCT", tbSearchNameCT.Text);
                dt = sql.getDataTable("sql_search_NhanVien", sqlPara);

                dt.Columns[0].ColumnName = "Mã nhân viên";
                dt.Columns[1].ColumnName = "Mã phòng ban";
                dt.Columns[2].ColumnName = "Tên phòng ban";
                dt.Columns[3].ColumnName = "Tên nhân viên";
                dt.Columns[4].ColumnName = "Địa chỉ";
                dt.Columns[5].ColumnName = "Ngày vào công ty";
                dt.Columns[6].ColumnName = "Ngày sinh";
                dt.Columns[7].ColumnName = "Mã công trình";
                dt.Columns[8].ColumnName = "Tên công trình";
                dt.Columns[9].ColumnName = "Mã công việc";
                dt.Columns[10].ColumnName = "Tên công việc";
                dt.Columns[11].ColumnName = "Ngày tham gia công việc";
                dt.Columns[12].ColumnName = "Ngày kết thúc công việc";
                dt.Columns[13].ColumnName = "Số ngày dự kiến hoàn thành công việc";                
                dataGridView1.DataSource = dt;                
            }
            getValueComboboxMPB(cbSearchMPB);
            getValueComboboxMNV(cbSearchMNV);
            getValueComboboxMCT(cbSearchMCT);
            cbSearchMCT.Text = strMCT;
            cbSearchMNV.Text = strMNV;
            cbSearchMPB.Text = strMPB;
            tbSearchNameCT.Text = strNCT;
            tbSearchNameNV.Text = strNNV;
            tbSearchNamePB.Text = strNPB;
        }

        public void getMaNhanVien()
        {
            try
            {               
                tbDKHT.ReadOnly = true;
                set_read_Only(false);
                clearTextBox();

                strQuery = sql.getDataTable("sql_select_NhanVien_OrderBy").Rows[0][0].ToString();
                strQuery = strQuery.Substring(2);
                int tmp = Convert.ToInt32(strQuery) + 1;
                if (tmp < 10)
                    tbMNV.Text = "NV0" + tmp;
                else
                    tbMNV.Text = "NV" + tmp;

                //get value MPB
                getValueComboboxMPB(cbMPB);
                getValueComboboxMCT_where(cbMCT);
                getValueComboboxMCV(cbMCV);
                cbMCV.Enabled = false;
            }
            catch
            {
            
            }
        }        
        #region get value into combobox

        //get value MPB into combobox
        public void getValueComboboxMPB(ComboBox cb)
        {
            //get value MPB into cbMPB            
            cb.DataSource = sql.getDataTable("sql_MPB_cb");
            cb.ValueMember = "MPB";
            cb.Text = "";
        }

        //get value MCT into Combobox
        public void getValueComboboxMCT(ComboBox cb) 
        {
            //string strTmp = cb.Text;
            cb.DataSource = sql.getDataTable("sql_MCT_cb");
            cb.ValueMember = "MCT";
            cb.Text = "";
        }

        //get value MCV into combobox
        public void getValueComboboxMCV(ComboBox cb)
        {
            //string strTmp = cb.Text;
            cb.DataSource = sql.getDataTable("sql_MCV_cb");
            cb.ValueMember = "MCV";
            cb.Text = "";
        }

        //get value MNV into combobox
        public void getValueComboboxMNV(ComboBox cb)
        {
            //string strTmp = cb.Text;
            SqlParameter sqlPara;
            if (rbNV.Checked == true)
                sqlPara = new SqlParameter("@checkNV", 1);
            else
                sqlPara = new SqlParameter("@checkNV", 0);
            cb.DataSource = sql.getDataTable("sql_MNV_cb", sqlPara);
            cb.ValueMember = "MNV";
            cb.Text = "";
        }     

        //get value MCT into combobox with condition
        public void getValueComboboxMCT_where(ComboBox cb)
        {           
            cb.DataSource = sql.getDataTable("sql_MCT_where_MPB_cb", new SqlParameter("@MPB", cbMPB.Text));
            cb.ValueMember = "MCT";
            cb.Text = "";            
        }

        #endregion
        
        private void themCongViecToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            frmAddCV frm = new frmAddCV();
            frm.getMNV(strMNV);            
            if (frm.ShowDialog() == DialogResult.OK)
            {
                sql_search_NhanVien();
            }
        }

        private void themCongTrinhToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmAddCT frm = new frmAddCT();
            frm.getMNV(strMNV);
            if (frm.ShowDialog() == DialogResult.OK)
            {
                sql_search_NhanVien();
            }
        }
    }
}
